﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace homework4
{
    public class Node<T>
    {
        public Node<T> Next
        {
            get;
            set;
        }
        public T Data
        {
            get;
            set;
        }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
                head = tail = n;
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action)
        {
            Node<T> flag = head;
            while(flag != null)
            {
                action(flag.Data);
                flag = flag.Next;
            }
        }
    }

    //测试
    public class Test
    {
        public static void inital(GenericList<int> numbers)
        {
            for (int i = 0; i < 5; i++)
            {
                numbers.Add(i);
            }
        }

        public static void Main(string[] args)
        {
            GenericList<int> numbers = new GenericList<int>();
            inital(numbers);

            //遍历打印
            numbers.ForEach(i => Console.WriteLine(i));

            //求最大值
            int max = 0;
            bool flag = false;
            numbers.ForEach(i =>
            {
                if (flag == false)
                {
                    max = i;
                    flag = true;
                }
                else
                {
                    if(i > max)
                    {
                        max = i;
                    }
                }
            });

            //求最小值
            int min = 0;
            bool flagmin = false;
            numbers.ForEach(i =>
            {
                if (flagmin == false)
                {
                    max = i;
                    flagmin = true;
                }
                else
                {
                    if (i < min)
                    {
                        max = i;
                    }
                }
            });

            //求和
            int sum = 0;
            numbers.ForEach(i => sum += i);

            Console.Read();
        }
    }
}
